package Sudoku3.rules;

import java.util.BitSet;

import Sudoku3.structure.bucket;
import Sudoku3.structure.cell;
import Sudoku3.structure.group;


public class rule06_hidden_set {
	
	//within a group, (n) cells have a subset of the same (n) possibilities
	public rule06_hidden_set(group g) {
				
		if (g instanceof bucket) { return; } //return if g is a bucket (this test is not valid).
		if (g.getSolvedCellCount() == 9) { return; } // return if the whole group is solved.
		
		//outer loop iterate cells
		for (cell c : g) {
			if (c.isSolved()) { continue; }
			bucket b = new bucket();
			b.add(c);
			
			//inner loop iterate other cells in group
			for (cell x : g) {
				if ( (x.isSolved()) || (x == c) ) { continue; }

				
				if ((c.toInt() | x.toInt()) == c.toInt()) {
					b.add(x); 
				}
			}
			
			//continue if we found only one cell
			if (b.size() == 1) { continue; }
			
			int count = 0;
			for (cell q : b) { 
				if (q.getPossible().cardinality() > count) {
					count = q.getPossible().cardinality();
				}
			}
			
			//number of cells that match have the same number of total possible values
			if (count == b.size()) {
				
				//in the bucket, get a string representing the cells labels and a bitset representing the total possibilities
				String s = b.toStringCells();
				BitSet m = new BitSet(9);
				for (cell y : b) { m.or(y.getPossible()); }
				
				//remove these value from all other cells in this group
				for (cell z : g) {
					
					if ( !(z.isSolved()) && !(b.contains(z)) && ( z.getPossible().intersects(m) ) ) {
						z.removePossible(m);
						System.out.println("Rule 06:  Hidden set found in " + g.toString() + " for cells(" + s + ") containing possibilities(" + m + ").  Removing these possibilities from cell(" + z.toStringCoords() + ").");
					}
				}
				
				
				
			}
			
			

		}
	}
}


